package com.ms.orm.dboperator;

import com.ms.orm.conditions.AbstractWrapper;
import com.ms.orm.context.DefaultLittleOrmContext;
import com.ms.orm.conditions.Wrapper;
import com.ms.orm.meta.IPage;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;

import java.io.Serializable;
import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * @author zdh
 */
@Configuration
@Component
public class BaseEntityDao implements DbOperatorAdaptor, InitializingBean {

    private DefaultLittleOrmContext defaultLittleOrmContext = DefaultLittleOrmContext.getDefaultLittleOrmContext();

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Override
    public void afterPropertiesSet() throws Exception {
        defaultLittleOrmContext.setJdbcTemplate(jdbcTemplate);
    }


    @Override
    public void save(Object entity) {
        defaultLittleOrmContext.getDefaultDbOperator(entity).save(entity);
    }

    @Override
    public int update(Object entity) {
        return defaultLittleOrmContext.getDefaultDbOperator(entity).updateById(entity);
    }

    @Override
    public List<Object> findAll(Class<?> entityClass) {
        return defaultLittleOrmContext.getDefaultDbOperator(entityClass).findAll();
    }

    @Override
    public Object findById(Class<?> entityClass, Serializable id) {
        return defaultLittleOrmContext.getDefaultDbOperator(entityClass).findById(id);
    }
    @Override
    public int deleteById(Class<?> entityClass, Serializable id) {
        return defaultLittleOrmContext.getDefaultDbOperator(entityClass).deleteById(id);
    }

    /**
     * 通过 Map 删除
     * @param columnMap 表字段 map 对象
     * @return
     */
    @Override
    public int deleteByMap(Class<?> entityClass,Map<String, Object> columnMap) {
        return defaultLittleOrmContext.getDefaultDbOperator(entityClass).deleteByMap(columnMap);
    }

    /**
     * 通过 updateWrapper 删除
     * @param updateWrapper 表字段 map 对象
     * @return
     */
    @Override
    public int delete(Class<?> entityClass,Wrapper<Object> updateWrapper){
        return defaultLittleOrmContext.getDefaultDbOperator(entityClass).delete(updateWrapper);
    }

    /**
     * 通过 updateWrapper 删除
     * @param entityClass 表字段 map 对象
     * @return
     */
    @Override
    public int deleteBatchIds(Class<?> entityClass, Collection<?> idList){
        return defaultLittleOrmContext.getDefaultDbOperator(entityClass).deleteBatchIds(idList);
    }

    /**
     * 更新
     * @param bean 设置值
     * @param updateWrapper 更新条件
     * @return
     */
    @Override
    public int update(Object bean,Wrapper updateWrapper){
        return defaultLittleOrmContext.getDefaultDbOperator(bean.getClass()).update(bean,updateWrapper);
    }

    /**
     * 通过 ids 查询
     * @param entityClass
     * @param idList
     * @return List<Object>
     */
    @Override
    public List<Object> selectBatchIds(Class<?> entityClass,Collection<? extends Serializable> idList){
        return defaultLittleOrmContext.getDefaultDbOperator(entityClass).selectBatchIds(idList);
    }
    @Override
    public List<Object> selectByMap(Class<?> entityClass,Map<String, Object> columnMap){
        return defaultLittleOrmContext.getDefaultDbOperator(entityClass).selectByMap(columnMap);
    }

    @Override
    public Object selectOne(Class<?> entityClass, Wrapper queryWrapper) {
        AbstractWrapper wrapper = (AbstractWrapper) queryWrapper;
        wrapper.setEntityClass(entityClass);
        return defaultLittleOrmContext.getDefaultDbOperator(entityClass).selectOne(wrapper);
    }
    @Override
    public Long selectCount(Class<?> entityClass,Wrapper queryWrapper){
        AbstractWrapper wrapper = (AbstractWrapper) queryWrapper;
        wrapper.setEntityClass(entityClass);
        return defaultLittleOrmContext.getDefaultDbOperator(entityClass).selectCount(wrapper);
    }

    @Override
    public List<Object> selectList(Class<?> entityClass, Wrapper queryWrapper) {
        AbstractWrapper wrapper = (AbstractWrapper) queryWrapper;
        wrapper.setEntityClass(entityClass);
        return defaultLittleOrmContext.getDefaultDbOperator(entityClass).selectList(queryWrapper);
    }

    @Override
    public  List<Map<String, Object>> selectMaps(Class<?> entityClass, Wrapper queryWrapper) {
        AbstractWrapper wrapper = (AbstractWrapper) queryWrapper;
        wrapper.setEntityClass(entityClass);
        return defaultLittleOrmContext.getDefaultDbOperator(entityClass).selectMaps(queryWrapper);
    }
    @Override
    public List<Object> selectOneColumnList(Class<?> entityClass,Wrapper<Object> queryWrapper){
        AbstractWrapper wrapper = (AbstractWrapper) queryWrapper;
        wrapper.setEntityClass(entityClass);
        return defaultLittleOrmContext.getDefaultDbOperator(entityClass).selectOneColumnList(queryWrapper);
    }


    /**
     * 分页查询
     * @param entityClass
     * @param page
     * @param queryWrapper
     * @param <P>
     * @return
     */
    @Override
    public <P extends IPage<Object>> P selectPage(Class<?> entityClass, P page, Wrapper<Object> queryWrapper){
        AbstractWrapper wrapper = (AbstractWrapper) queryWrapper;
        wrapper.setEntityClass(entityClass);
        return defaultLittleOrmContext.getDefaultDbOperator(entityClass).selectPage(page,queryWrapper);
    }

    /**
     * 根据 Wrapper 条件，查询全部记录（并翻页）
     * @param page 分页查询条件
     * @param queryWrapper 实体对象封装操作类
     * @param <P>
     * @return
     */
    @Override
    public <P extends IPage<Map<String, Object>>> P selectMapsPage(Class<?> entityClass, P page,Wrapper<Object> queryWrapper){
        AbstractWrapper wrapper = (AbstractWrapper) queryWrapper;
        wrapper.setEntityClass(entityClass);
        return defaultLittleOrmContext.getDefaultDbOperator(entityClass).selectMapsPage(page,queryWrapper);
    }

    /**
     * 批量插入
     * @param entityClass
     * @param list
     * @return
     */
    @Override
    public int[] batchSave(Class<?> entityClass,List<Object> list){
        return defaultLittleOrmContext.getDefaultDbOperator(entityClass).batchSave(list);
    }
}
